<template>
  <div class="padding-l-20 padding-r-20 padding-t-20 h-100 table-page">
    <filter-bar>
      <template slot="left-bar">
        <a-space>
          <a-input
            class="position-r"
            placeholder="请输入关键字搜索"
            v-model="searchForm.word"
          />
          <a-button class="nbcs-btn-search" @click="page.currentPage = 1;getList(page, searchForm)">查询</a-button>
          <a-button class="nbcs-btn-default" @click="clearObj(searchForm)">重置</a-button>
        </a-space>
      </template>
    </filter-bar>
    <interval-bg></interval-bg>
    <opt-bar>
      <div slot="left-bar">
        黑名单词库
      </div>
      <div slot="right-bar">
        <a-button
          @click="edit(0)"
          class="nbcs-btn-main-bg"
          style="top:-1.6px"
          v-if="permission('sys:sensitiveWord:add')"
          >新增</a-button>
      </div>
    </opt-bar>
    <a-table
      ref="_table"
      bordered
      :columns="columns"
      :data-source="list"
      :pagination="pageConfig"
      rowKey="id"
      :loading="loading"
      :scroll="{ x: 1000, y: tableHeight }"
    >
      <template slot="roles" slot-scope="text">
        <a-tag v-for="(item,index) in text" :key="index">{{item.name}}</a-tag>
      </template>
      <span slot="tags" slot-scope="text, record">
        <a-tag v-if="record.enabled" class="tag-green">
          正常
        </a-tag>
        <a-tag v-else class="tag-red">
          锁定
        </a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
      <a-space size="middle">
        <a @click="edit(2, record)" >查看</a>
        <a @click="edit(1, record)"  v-if="permission('sys:sensitiveWord:update')">编辑</a>
        <a-popconfirm title="确定删除该词汇吗？" @confirm="del(record)" v-if="permission('sys:sensitiveWord:delete')">
          <a-icon slot="icon" type="question-circle-o" style="color: red" />
          <a class="table-red-btn">删除</a>
        </a-popconfirm>
      </a-space>
      </span>
    </a-table>
    <word-form ref="wordForm" @getList="getList(page, searchForm)"></word-form>
  </div>
</template>

<script>
import FilterBar from "@/layouts/FilterBar.vue";
import IntervalBg from "@/layouts/IntervalBg";
import OptBar from "@/layouts/OptBar";
import { list, del} from "@/services/word";
import WordForm from "./wordForm";
import {cutDateSecond} from '@/utils/date'
import {clearObj} from '../../utils/util';
import { itemList } from "@/services/word";
export default {
  name: "WordList",
  components: { FilterBar, WordForm, IntervalBg, OptBar},
  data() {
    return {
      tableHeight: 0,
      scrollY: 0,
      loading: false,
      searchForm: {
          word:"",
      },
      list: [],
      columns: [
        { title: "黑名单词汇",dataIndex: "word" },
        {
          title: "错误等级",
          dataIndex: "errorLevelText"
        },
        {
          title: "错误类型",
          dataIndex: "errorTypeText"
        },
        // {
        //   title: "敏感类型",
        //   dataIndex: "wordType",
        // },
        // {
        //   title: "词库类型",
        //   dataIndex: "sensitiveType",
        // },
        {
          title: "创建时间",
          dataIndex: "createTime",
          customRender:text=>{
            return cutDateSecond(text)
          }},
        {
          title: "创建人员",
          dataIndex: "createUserName",
        },
        {
          title: "操作",
          align: 'center',
          key: "operation",
          fixed: "right",
          width: 180,
          scopedSlots: { customRender: "action" }
        }
      ],
      pageConfig: {
        showQuickJumper: true,
        showSizeChanger: true,
        hideOnSinglePage: false, //只有一页时是否隐藏分页器
        defaultPageSize: 20,
        pageSizeOptions: ["20", "30", "50"],
        total: 0,
        current: 1,
        showTotal: total => {
          return `共 ${total} 条`;
        },
          onShowSizeChange: (page, pageSize) => {
              this.page.currentPage = 1;
              this.page.pageSize = pageSize;
              this.getList(this.page,this.searchForm);
          },
          onChange: (page) => {
              this.page.currentPage =page;
              this.pageConfig.current = page;
              this.getList(this.page,this.searchForm);
          },
      },
      page: {
        currentPage: 1,
        pageSize: 20
      }
    };
  },
  computed: {
  },
  created() {
    this.$nextTick(() => {
        this.getTableScroll()
    })
    this.getList(this.page);
  },
  mounted() {
      this.tableHeight = document.querySelector('.table-page').clientHeight - 276;
  },
  methods: {
    getTableScroll() {
      let wrapperHeight = this.$refs._table.$el.offsetHeight;
      this.scrollY = wrapperHeight - 100;
    },
    permission(name) {
      let permissions = this.$store.getters["account/permissions"];
      return permissions.filter(m => m.id == name || m.operation.indexOf(name) >= 0).length > 0
    },
    getList(page, params) {
      this.loading = true;
      list(
        Object.assign(
          {
            page: page.currentPage,
            pageSize: page.pageSize,
            sensitiveType:'BLACK'
          },
          params
        )
      ).then(async res => {
          this.loading = false;
        const { data } = res.data;
        let el = (await itemList({ type: 'blackword_errorlevel' })).data.data;
        let et = (await itemList({ type: 'blackword_errortype' })).data.data;
        this.list = data.list.map(m=> {
          let al = el.filter(n => n.value == m.errorLevel);
          let at = et.filter(n => n.value == m.errorType);
          m.errorLevelText = al.length > 0 ? al[0].label:'';
          m.errorTypeText =  at.length>0? at[0].label:'';
          return m;
        });
          this.pageConfig.current = data.page;
          this.pageConfig.total = data.totalRow;
      });
    },

    del(obj) {
      del({ id: obj.id }).then(res => {
        if (res.data.code === 0) {
          this.$message.success("删除成功");
          this.getList(this.page);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //添加/编辑
    edit(action, data = {}){
      this.$refs['wordForm'].changeForm(true,action,data);
    },
    clearObj
  }
};
</script>

<style lang="less" scoped>
.select {
  width: 170px;
  /deep/ .ant-select-selection {
    width: 100%;
  }
}
._input {
  width: 180px;
}
.table-page {
  .ant-table-wrapper {
    height: calc(100% - 150px);
  }
}
</style>
